package svgedit.gui.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;

import svgedit.gui.Frame;
import svgedit.gui.View;
import svgedit.svg.SVGElement;

/**
 * SelectAllAction selects all elements when MenuItem->SelectAll is pressed
 * 
 * @author SARJ
 * 
 */
public class SelectAllAction extends AbstractAction implements ActionListener
{

	private static final long serialVersionUID = 2111352089259462683L;

	/**
	 * Construct a new Select all menu item
	 */
	public SelectAllAction()
	{
		super("Select All");
	}

	/**
	 * Listenes for the Select All button
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		Frame f = Frame.getFrame();
		View v = f.getView();

		selectAll(f, v);
		
		Frame.getFrame().enableDeleteMenuItem(true);
		Frame.getFrame().enableGroupMenuItem(true);
		
		if (v.getSelected().includesGroups())
			Frame.getFrame().enableUnGroupMenuItem(true);
	}

	/**
	 * 
	 * @param f
	 *            - the Frame {@link svgedit.gui.Frame}
	 * @param v
	 *            - the View {@link svgedit.gui.View}
	 */
	private void selectAll(Frame f, View v)
	{

		for (SVGElement e : f.getDocument())
		{
			if (e instanceof svgedit.svg.SVGGroup)
			{
				// dont need to do anything with groups
			}
			else
			{
				v.insertIntoSelection(e);
				v.repaint();
			}
		}
	}
}
